<?php

App::uses('CrudController', 'Controller');
App::uses('Procedure', 'Model');

class SurveysController extends CrudController {

    public $_model = 'Survey';

    public function admin($workstationConfigId, $workstationId, $lineId, $areaId, $moeId) {
        $this->request->allowMethod('get');
        $description = $this->getDescription($workstationConfigId, $workstationId, $lineId, $areaId, $moeId);
        $appBosch = new stdClass();
        $appBosch->workstationConfigId = $workstationConfigId;
        $appBosch->workstationId = $workstationId;
        $appBosch->lineId = $lineId;
        $appBosch->areaId = $areaId;
        $appBosch->moeId = $moeId;
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Cuestionario'));
        $this->set('description', $description);
    }

    private function getDescription($workstationConfigId, $workstationId, $lineId, $areaId, $moeId) {
        $this->loadModel('Area');
        $this->loadModel('Moe');
        $this->loadModel('Line');
        $this->loadModel('Workstation');
        $this->loadModel('WorkstationConfig');
        $this->Moe->id = $moeId;
        $moe = $this->Moe->read();
        $this->Area->id = $areaId;
        $area = $this->Area->read();
        $this->Line->id = $lineId;
        $line = $this->Line->read();
        $this->Workstation->id = $workstationId;
        $workstation = $this->Workstation->read();
        $this->WorkstationConfig->id = $workstationConfigId;
        $workstationConfig = $this->WorkstationConfig->read();
        $parts = array(
            $moe['Moe']['name'],
            $area['Area']['name'],
            $line['Line']['name'],
            $workstation['Workstation']['description'],
            $workstationConfig['WorkstationConfig']['name'],
        );
        return implode(' - ', $parts);
    }

    public function apply($workstationConfigId, $userId)
    {
        $this->request->allowMethod('get');
        $this->loadModel('WorkstationConfig');
        $this->loadModel('User');
        $m = $this->_model;
        $questions = $this->$m->getEnabledByWorkstationConfig($workstationConfigId);
        $this->WorkstationConfig->id = $workstationConfigId;
        $wc = $this->WorkstationConfig->read();
        $appBosch = new stdClass();
        $appBosch->userId = $userId;
        $appBosch->questions = $this->groupQuestions($questions);
        $appBosch->numQuestions = count($questions);
        $appBosch->breadcrumb = $this->WorkstationConfig->getBreadcrumb($workstationConfigId);
        $appBosch->minQualification = (integer)$wc['WorkstationConfig']['minimum_qualification'];
        $workstationConfigName = $appBosch->breadcrumb['WorkstationConfig']['name'];
        $this->set('appBosch', $appBosch);
        $this->set('title', $appBosch->breadcrumb['w']['name']);
        $this->set('description', $workstationConfigName);
    }

    private function groupQuestions($questions)
    {
        $groups = array();
        foreach ($questions as $question) {
            if (isset($groups[$question['group']]) === false) {
                $groups[$question['group']] = array();
            }
            $groups[$question['group']][] = $question;
        }
        return $groups;
    }

    public function x($workstationConfigId, $userId)
    {
        $this->request->allowMethod('get');
        $this->loadModel('WorkstationConfig');
        $this->loadModel('User');
        $this->loadModel('SurveyFolio');
        $this->loadModel('Procedure');
        $m = $this->_model;
        $questions = $this->$m->getEnabledByWorkstationConfig($workstationConfigId);
        $this->Procedure->id = Procedure::P0007Q;
        $procedure = $this->Procedure->read();
        $this->WorkstationConfig->id = $workstationConfigId;
        $wc = $this->WorkstationConfig->read();
        $this->User->id = $userId;
        $user = $this->User->read();
        $appBosch = new stdClass();
        $appBosch->questions = $this->groupQuestions($questions);
        $appBosch->numQuestions = count($questions);
        $appBosch->breadcrumb = $this->WorkstationConfig->getBreadcrumb($workstationConfigId);
        $appBosch->user = $user;
        $appBosch->supervisor = $this->Auth->user('full_name');
        $appBosch->folio = $this->SurveyFolio->getFolio();
        $appBosch->minQualification = (integer) $wc['WorkstationConfig']['minimum_qualification'];
        $workstationConfigName = $appBosch->breadcrumb['WorkstationConfig']['name'];
        $procedure['Procedure']['description'] = $procedure['Procedure']['description'] . ' ' . $workstationConfigName;
        $appBosch->procedure = $procedure['Procedure'];
        $this->set('appBosch', $appBosch);
        $this->set('title', $appBosch->breadcrumb['w']['name']);
        $this->set('description', $workstationConfigName);
    }

    protected function getRecords($param)
    {
        $m = $this->_model;
        $records = $this->$m->getEnabledByWorkstationConfig($param);
        return $records;
    }

    protected function c($model)
    {
        return array(
            'orden' => (int)$model->orden,
            'question' => mb_strtoupper(trim($model->question)),
            'group' => mb_strtoupper(trim($model->group)),
        );
    }

    protected function u($model)
    {
        return array(
            'orden' => (int)$model->orden,
            'question' => mb_strtoupper(trim($model->question)),
            'group' => mb_strtoupper(trim($model->group)),
        );
    }

}
